package com.itheima.sfbx.insurance.handler.impl;

import com.itheima.sfbx.framework.commons.constant.insure.InsureConstant;
import com.itheima.sfbx.insurance.dto.CustomerRelationVO;
import com.itheima.sfbx.insurance.dto.DoInsureVo;
import com.itheima.sfbx.insurance.handler.InsureHandler;
import com.itheima.sfbx.insurance.service.ICustomerRelationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @ClassName AccidentInsureHandler.java
 * @Description 保障型意外类：保险投保、创建订单、保费计算
 */
@Service
public class AccidentInsureHandler extends SafeguardInsureHandler implements InsureHandler {

    @Resource
    private ICustomerRelationService customerRelationService;

    @Override
    public String doPremium(DoInsureVo doInsureVo) {
        //1.获取被保险人id
        String id = doInsureVo.getCustomerRelationIds().get(0);
        //2.根据id获取被保险人信息
        CustomerRelationVO customerRelationVO = customerRelationService.findById(id);
        //3.查询被保险人的收入，为空或者月入过百万，则抛出异常
        if(customerRelationVO != null){
            if(customerRelationVO.getIncome() == null || customerRelationVO.getIncome()
                    .compareTo(new BigDecimal(1000000)) > 0){
                throw new RuntimeException("杀手不可以买意外保险");
            }
        }
        return super.doPremium(doInsureVo);
    }
}
